import requests
from bs4 import BeautifulSoup
import bs4
import re
import urllib.request,urllib.error
import requests
import json
import peewee
from peewee import*
import datetime
datalist = []
def main():
    for i in range(1, 66):
        getdata(i)
    print("已获取数据")
    # print(datalist)
    savedata(datalist)
    print("已保存数据")

def getdata(page):
    headers = {
        'User - Agent': 'Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 89.0.4389.90 Safari / 537.36 Edg / 89.0.774.54'
    }
    num = 20
    # 最后一页不足20条
    if(page == 65):
        num = 5
    page = str(page)
    # 发送请求获取页面的html
    info = requests.get(url='https://info.scau.edu.cn/3772/list'+page+'.htm', headers=headers)
    info.encoding = 'utf-8'
    # 用BeautifulSoup解析html
    html = BeautifulSoup(info.text, 'html.parser')
    # print(html)
    # result = []
    # 循环拿对应的新闻的标题、日期和链接
    for i in range(0, num):
        title = html.select('.title')[i].text
        date1 = html.select('.date')[i].text
        desc = html.select('.desc')[i].find('a').text
        # a = targetUrl.get('href')
        # prefixUrl = 'https://info.scau.edu.cn'
        # targetUrl = prefixUrl + a
        # print(targetUrl)
        datalist.append({
            "title": title,
            "date": date1,
            "desc": desc
        })
    print(page)
    # return result
        # print(result)


def savedata(data):
    db = MySQLDatabase('scauInfo', host='127.0.0.1', user='mysql用户名', passwd='mysql密码')
    db.connect()
    class BaseModel(Model):
        class Meta:
            database = db  # 每一个继承BaseModel类的子类都是连接db表

    class Info(BaseModel):
        title = CharField()
        date = CharField()
        desc = CharField()

    Info.create_table()
    i = 0
    length = len(data)
    # print(data[0])
    # print(data[0]['title'])
    while i < length:
        Info.create(title=data[i]['title'], date=data[i]['date'], desc=data[i]['desc'])
        i += 1
if __name__ == "__main__":  # 当程序执行时
    # 调用函数
    main()
